package ip

import "os"

func valueOfFirstNonEmptyEnv(keyArr ...string) string {
	for _, key := range keyArr {
		value := os.Getenv(key)
		if value != "" {
			return value
		}
	}
	return ""
}

func Intranet(ipv6only bool) string {
	if ipv6only {
		// see https://bytedance.feishu.cn/wiki/wikcnR7f17phbl2h7ZVnj4KgRfg
		return valueOfFirstNonEmptyEnv(
			"MY_HOST_IPV6", "TCE_HOST_IPV6", // ipv6
			"MY_HOST_IP", "TCE_HOST_IP", // ipv4
		)
	}

	return valueOfFirstNonEmptyEnv("MY_HOST_IP", "TCE_HOST_IP")
}
